package org.oos.atom.entries.impl;

import static org.oos.atom.elements.Category.Type.ATTRIBUTES;
import static org.oos.atom.elements.Category.Type.CATEGORIES;
import static org.oos.atom.elements.Category.Type.TAGS;

import java.util.ArrayList;
import java.util.List;

import org.oos.atom.elements.AbstractOosEntry;
import org.oos.atom.elements.Area;
import org.oos.atom.elements.Category;
import org.oos.atom.elements.GeoLocation;
import org.oos.atom.elements.Link;
import org.oos.atom.elements.OwnedByUser;
import org.oos.atom.elements.Rating;
import org.oos.atom.elements.SavedBy;
import org.oos.atom.elements.Text;
import org.oos.atom.entries.SiteEntry;
import org.oos.model.OosImage;
import org.oos.model.SiteLocation;

import com.google.api.client.util.Key;

/**
 * TODO: comments
 * 
 * @author pablo.gonzalez@11870.com
 * 
 */
public class SiteEntryImpl extends AbstractOosEntry implements SiteEntry,
		Cloneable {

	private static final long serialVersionUID = 3446370356612541847L;

	@Key("oos:id")
	private Integer oosId;

	@Key("oos:reviews-counter")
	private Integer reviews;

	@Key("oos:saved-counter")
	private Integer saves;

	private List<OosImage> images;
	private List<Category> siteTags;
	private List<Category> oosCategories;
	private List<Category> attributes;

	@Key("oos:telephone")
	private String telephone;

	@Key("oos:slug")
	private String slug;

	@Key("oos:url")
	private String url;

	@Key("oos:rating")
	protected Rating rating;

	@Key("georss:where")
	private GeoLocation location;
	private SiteLocation oosLocation;

	@Key("oos:useraddress")
	private String userAddress;

	@Key("oos:locality")
	protected Area locality;

	@Key("oos:country")
	protected Area country;

	@Key("oos:saved-by")
	protected SavedBy savedBy;

	@Key("oos:owned-by-user")
	protected OwnedByUser ownedByUser;
	 
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.oos.datasource.feed.SearchEntry#getImages()
	 */
	@Override
	public List<OosImage> getImages() {
		if (images == null) {
			List<Link> links = Link.find(getLinks(), "media");
			images = new ArrayList<OosImage>();
			OosImage image;
			for (Link link : links) {
				image = OosImage.create(link.getHref(), link.getType());
				if (image != null)
					images.add(image);
			}
		}
		return images;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.oos.datasource.feed.SearchEntry#getReviewCount()
	 */
	@Override
	public Integer getReviewCount() {
		return reviews;
	}
	
	@Override
	public void setReviewCount(Integer count) {
		this.reviews = count;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.oos.datasource.feed.SearchEntry#getSavesCount()
	 */
	@Override
	public Integer getSaveCount() {
		return saves;
	}
	
	@Override
	public void setSaveCount(Integer count) {
		this.saves = count;
	}

	@Override
	public List<Category> getTags() {
		if (siteTags == null) {
			siteTags = Category.find(super.getCategories(), TAGS);
		}
		return siteTags;
	}

	@Override
	public List<Category> getCategories() {
		if (oosCategories == null) {
			oosCategories = Category.find(super.getCategories(), CATEGORIES);
		}
		return oosCategories;
	}

	@Override
	public List<Category> getAttributes() {
		if (attributes == null) {
			attributes = Category.find(super.getCategories(), ATTRIBUTES);
		}
		return attributes;
	}

	@Override
	public SiteLocation getLocation() {
		if (oosLocation == null) {
			oosLocation = new SiteLocation();

			oosLocation.setAddress(userAddress);
			oosLocation.setLocality(locality);
			oosLocation.setCountry(country);

			if (this.location != null) {
				oosLocation.setLatitude(this.location.getLatitude());
				oosLocation.setLongitude(this.location.getLongitude());
			}
		}

		return oosLocation;
	}
    
	@Override
    public void setLocation(SiteLocation location) {
		this.oosLocation = location;
		this.userAddress = location.getAddress();
		this.locality = location.getLocality();
		this.country = location.getCountry();
		this.location = GeoLocation.create(location.getLatitude(), location.getLongitude());
	}

	@Override
	public String getName() {
		return this.title.getValue();
	}
	
	@Override
    public void setName(String name) {
		this.title = new Text(name);
	}

	@Override
	public String getPhone() {
		return this.telephone;
	}
    
	@Override
    public void setPhone(String phone) {
		this.telephone = phone;
	}

	@Override
	public String getSlug() {
		return this.slug;
	}

	@Override
	public void setSlug(String slug) {
		this.slug = slug;
	}

	@Override
	public Integer getOosId() {
		return this.oosId;
	}
	
	@Override
	public void setOosId(Integer id) {
		this.oosId = id;
	}

	@Override
	public String getUrl() {
		return this.url;
	}
    
	@Override
    public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public Rating getRating() {
		return this.rating;
	}
    
	@Override
    public SavedBy getSavedBy() {
		return savedBy;
	}
    
	@Override
    public OwnedByUser getOwnedByUser() {
		return ownedByUser;
	}
    
	@Override
    public void setOwnedByUser(OwnedByUser owned) {
		this.ownedByUser = owned;
	}

	@Override
	public SiteEntryImpl clone() throws CloneNotSupportedException {
		return (SiteEntryImpl) super.clone();
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof SiteEntry) {
			SiteEntry other = (SiteEntry) obj;
			return other.getSlug().equals(getSlug());
		}
		return false;
	}
}
